package net.zwet.clanworks.connectors.eve.api.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {
	public static String EvEDateFormatString = "yyyy-MM-dd HH:mm:ss";
	public static String EvETimeFormatString = "HH:mm:ss";

	/**
	 * Convert a string to a date. 
	 * The EvE API provides dates in the format "yyyy-MM-dd HH:mm:ss".
	 * 
	 * @param date
	 * @param formatString
	 * @return
	 */
	public static Date convertToDate ( String date, String formatString ) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
		
		Date convertedDate;
		try {
			convertedDate = dateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

		return convertedDate;	
	}
	
	/**
	 * Convert a given time to EvE Time.
	 * EvE Time happens to be in equal to GMT ... so use that as the timezone to convert to.
	 * 
	 * @param date
	 * @return date
	 */
	public static Date eveTime ( Date date ) {
		TimeZone _timeZone = TimeZone.getDefault();
		int timeZoneOffset = ( _timeZone.getOffset(date.getTime()) / 3600000 ); // offset in hours instead of milliseconds

		// EvE time happens to be in sync with GMT ... how convenient.
		Calendar calEvE = Calendar.getInstance();
		calEvE.setTime(date);
		calEvE.add(Calendar.HOUR, (-1 * timeZoneOffset) ); // Adjust offset to GMT

		return calEvE.getTime();
	}
	
	public static Date eveTime() {
		return eveTime( new Date() );
	}
}
